"""
数据处理
"""


import json
import os

from config import settings


def select(username: str) -> dict:
    """获取用户信息"""
    # 拼接用户数据文件路径
    user_data_path = os.path.join(
        settings.USERS_DATA_PATH, f'{username}.json'
    )

    # 判断用户数据文件是否存在
    if os.path.exists(user_data_path):

        # 读取用户信息
        with open(user_data_path, 'r', encoding='utf-8') as f:
            user_dic = json.load(f)
        return user_dic

    # 没有返回值，默认返回None


def save(username: str, user_dict: dict) -> None:
    """保存用户信息"""
    # 拼接用户数据文件路径
    user_data_path = os.path.join(
        settings.USERS_DATA_PATH, f'{username}.json'
    )

    with open(user_data_path, 'w', encoding='utf-8') as f:
        # ensure_ascii=False使非ASCII字符，以正常格式显示
        json.dump(user_dict, f, ensure_ascii=False)
